package com.ruoyi.web.controller.analysis;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.service.analysis.IUserAnalysisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
/**
 * 用户分析控制器
 */
@RestController
@RequestMapping("/analysis/user")
public class UserAnalysisController extends BaseController {

    @Autowired
    private IUserAnalysisService userAnalysisService;

    /**
     * 获取用户分析仪表板数据
     */
    @PreAuthorize("@ss.hasPermi('analysis:user:list')")
    @GetMapping("/dashboard")
    public AjaxResult getDashboardData() {
        Map<String, Object> data = userAnalysisService.getDashboardData();
        return AjaxResult.success(data);
    }

    /**
     * 获取增强版仪表板数据（7个可视化）
     */
    @PreAuthorize("@ss.hasPermi('analysis:user:list')")
    @GetMapping("/enhanced-dashboard")
    public AjaxResult getEnhancedDashboardData() {
        Map<String, Object> data = userAnalysisService.getEnhancedDashboardData();
        return AjaxResult.success(data);
    }

    /**
     * 获取用户分层数据
     */
    @PreAuthorize("@ss.hasPermi('analysis:user:list')")
    @GetMapping("/tier")
    public AjaxResult getUserTierData() {
        return AjaxResult.success(userAnalysisService.getUserTierDistribution());
    }

    /**
     * 获取行为特征数据
     */
    @PreAuthorize("@ss.hasPermi('analysis:user:list')")
    @GetMapping("/behavior")
    public AjaxResult getBehaviorData() {
        return AjaxResult.success(userAnalysisService.getBehaviorRadarData());
    }

    /**
     * 获取时间趋势数据
     */
    @PreAuthorize("@ss.hasPermi('analysis:user:list')")
    @GetMapping("/trend")
    public AjaxResult getTimeTrendData() {
        return AjaxResult.success(userAnalysisService.getTimeTrendData(6));
    }

    // 添加新的API端点
    @GetMapping("/actor-preferences")
    public AjaxResult getActorPreferences() {
        List<Map<String, Object>> data = userAnalysisService.getActorPreferences();
        return AjaxResult.success(data);
    }

    @GetMapping("/rfm-analysis")
    public AjaxResult getRFMAnalysis() {
        List<Map<String, Object>> data = userAnalysisService.getRFMAnalysis();
        return AjaxResult.success(data);
    }

    /**
     * 获取单个可视化数据（按需加载）
     */
    @PreAuthorize("@ss.hasPermi('analysis:user:list')")
    @GetMapping("/visualization/{type}")
    public AjaxResult getVisualizationData(@PathVariable String type) {
        Map<String, Object> data = new HashMap<>();
        switch (type) {
            case "pyramid":
                data.put("pyramid", userAnalysisService.getUserPyramidData());
                break;
            case "3d-analysis":
                data.put("threeDimensional", userAnalysisService.getThreeDimensionalData());
                break;
            case "funnel":
                data.put("paymentFunnel", userAnalysisService.getPaymentFunnelData());
                break;
            case "heatmap":
                data.put("watchHeatmap", userAnalysisService.getWatchHeatmapData());
                break;
            case "preference":
                data.put("contentPreference", userAnalysisService.getContentPreferenceData());
                break;
            case "lifecycle":
                data.put("userLifecycle", userAnalysisService.getUserLifecycleData());
                break;
            case "revenue":
                data.put("revenueContribution", userAnalysisService.getRevenueContributionData());
                break;
            default:
                return AjaxResult.error("未知的可视化类型");
        }
        return AjaxResult.success(data);
    }
}